// ====================StateInfoSprite====================

class StateInfoPanel extends CardContainerBase {
    initialize() {
        super.initialize();
        this._damages = [];
        this.location();
        this.createAllSprite();
    }

    /**
     * 定位
     */
    location() {
        const {x, y} = SDS_CARD_PLUGIN_PARAM.gameUI.panelRect;
        this.move(x - 4, y + 20);
    }

    createAllSprite() {
        this.createBackgroundSprite();
        this.createFaceSprite();
        this.createGaugeSprite();
        this.createEnergySprite();
        this.createParamSprite();
        this.createCemeterySprite();
        this.createCemeteryCardListSprite();
    }

    /**
     * 创建背景精灵
     */
    createBackgroundSprite() {
        const fileName = SDS_CARD_PLUGIN_PARAM.gameUI.backgroundFile;
        let bitmap;
        if (!!fileName) {
            bitmap = ImageManager.loadSystem(fileName);
        } else {
            const {width, height} = SDS_CARD_PLUGIN_PARAM.gameUI.panelRect;
            bitmap = new Bitmap(width, height);
            bitmap.fontSize = 50;
            // 最底层
            bitmap.fillRoundRect(0, 0, width, height, "#023d7c", 0);
            bitmap.fillRoundRect(0, 0, width, 5, "#fcdb02", 0);
            // // 攻击
            // bitmap.fillRoundRect(180, 15, 125, 40, "#01356c", 0);
            // bitmap.drawIcon(97, 184, 19, 32, 32);
            // // 防御
            // bitmap.fillRoundRect(180, 60, 125, 40, "#01356c", 0);
            // bitmap.drawIcon(129, 184, 64, 32, 32);
            // // =====装备=====
            // // 武器
            // bitmap.fillRoundRect(180, 105, 125, 40, "#01356c", 0);
            // bitmap.drawIcon(129, 184, 64, 32, 32);
        }
        const sprite = new Sprite(bitmap);
        this.addChild(sprite);
        this._backgroundSprite = sprite;
    }

    /**
     * 创建脸图精灵
     */
    createFaceSprite() {
        const {x, y, width, height} = SDS_CARD_PLUGIN_PARAM.gameUI.faceRect;
        const bitmap = new Bitmap(width, height);
        const sprite = new Sprite(bitmap);
        const faceFileName = $gameParty.battleMembers()[0].faceName();
        const faceIndex = $gameParty.battleMembers()[0].faceIndex();
        bitmap.drawFace(faceFileName, faceIndex, 0, 0, width, height);
        sprite.move(x, y);
        this.addChild(sprite);
        this._faceSprite = sprite;
        CardGameManager.face = sprite;
    }

    createGaugeSprite() {
        const battler = $gameParty.battleMembers()[0];
        const hpSprite = new Sprite_Gauge();
        // const mpSprite = new Sprite_Gauge();
        hpSprite.setup(battler, "hp");
        // mpSprite.setup(battler, "mp");
        hpSprite.move(16, 170)
        // mpSprite.move(16, 200)
        this.addChild(hpSprite);
        // this.addChild(mpSprite);
    }

    /**
     * 创建能量精灵
     */
    createEnergySprite() {
        const width = 285;
        const height = 32;
        const x = 16;
        const y = 200;
        const sprite = new CardEnergy({width, height, x, y});
        this.addChild(sprite);
        this._energySprite = sprite;
    }

    createParamSprite() {
        const width = 285;
        const height = 280;
        const bitmap = new Bitmap(width, height);
        const sprite = new Sprite(bitmap);
        const battler = $gameParty.battleMembers()[0];
        bitmap.fontSize = 32;
        bitmap.fontFace = $gameSystem.mainFontFace();
        bitmap.drawText(battler.atk, 0, 19, 285, 40, "right");
        bitmap.drawText(battler.def, 0, 64, 285, 40, "right");
        this.addChild(sprite);
    }

    /**
     * 创建墓地精灵
     */
    createCemeterySprite() {
        const width = 100;
        const height = 50;
        const bitmap = new Bitmap(width, height);
        const sprite = new Sprite(bitmap);
        bitmap.fillRoundRect(0, 0, width, height, "#77690f", 0);
        bitmap.fontSize = 35;
        bitmap.drawText("外挂", 0, 0, 100, 50, "center");
        sprite.move(1240, 20)
        this.addChild(sprite);
        this._cemeterySprite = sprite;
    }

    createCemeteryCardListSprite() {
        const bitmap = new Bitmap();
        const sprite = new Sprite(bitmap);
        sprite.hide();
        this.addChild(sprite);
        this._cemeteryCardListSprite = sprite;
    }

    /**
     * 刷新墓地
     */
    refreshCemetery() {
        const cemetery = CardGameManager.cemetery;
        const deck = CardGameManager.deck;
        const listAmount = Math.max(cemetery.length, deck.length);
        const width = 200;
        const height = 20 * listAmount + 30;
        const sprite = this._cemeteryCardListSprite;
        const bitmap = sprite.bitmap;
        // bitmap.clear();
        bitmap.resize(width, height);
        bitmap.fillRoundRect(0, 0, width, height, "#adadad", 0);
        bitmap.drawText("牌库(按顺序)：", 5, 5, width, 20, "left");
        bitmap.drawText("墓地：", 105, 5, width, 20, "left");
        let x = 5, y = 25;
        for (const card of deck) {
            const name = card.name();
            bitmap.drawText(name, x, y, width, 20, "left");
            y += 20;
        }
        x = 105, y = 25;
        for (const card of cemetery) {
            const name = card.name();
            bitmap.drawText(name, x, y, width, 20, "left");
            y += 20;
        }
        sprite._onBitmapChange();
        // 根据大小调整位置
        sprite.move(Graphics.boxWidth - width, -height + 50);
    }

    update() {
        super.update();
        this.updateCemetery();
        this.updateDamagePopup();
    }

    /**
     * 更新伤害弹出
     */
    updateDamagePopup() {
        this.setupDamagePopup();
        if (this._damages.length > 0) {
            for (const damage of this._damages) {
                damage.update();
            }
            if (!this._damages[0].isPlaying()) {
                this.destroyDamageSprite(this._damages[0]);
            }
        }
    }

    /**
     * 更新墓地
     */
    updateCemetery() {
        // 判断鼠标在不在墓地范围内
        const pos = new Point(TouchInput.x, TouchInput.y);
        // const localPos = this.worldTransform.applyInverse(touchPos);
        const cemeteryRect = this._cemeterySprite.getBounds();
        const hit = cemeteryRect.contains(pos.x, pos.y);
        if (!!hit) {
            if (!this._cemeteryCardListSprite.visible) {
                this.refreshCemetery();
            }
            this._cemeteryCardListSprite.show();
        } else {
            this._cemeteryCardListSprite.hide();
        }
    }

    /**
     * 设置伤害弹出
     */
    setupDamagePopup() {
        const battler = $gameParty.battleMembers()[0];
        if (battler.isDamagePopupRequested()) {
            this.createDamageSprite();
            battler.clearDamagePopup();
            battler.clearResult();
        }
    }

    /**
     * 创建伤害弹出
     */
    createDamageSprite() {
        const battler = $gameParty.battleMembers()[0];
        const last = this._damages[this._damages.length - 1];
        const sprite = new Sprite_Damage();
        if (last) {
            sprite.x = last.x + 8;
            sprite.y = last.y - 16;
        } else {
            sprite.x = this.x + this.damageOffsetX();
            sprite.y = this.y + this.damageOffsetY();
        }
        sprite.setup(battler);
        this._damages.push(sprite);
        this.parent.addChild(sprite);
    }

    /**
     * 销毁伤害弹出
     */
    destroyDamageSprite(sprite) {
        this.parent.removeChild(sprite);
        this._damages.remove(sprite);
        sprite.destroy();
    }

    /**
     * 伤害的偏移坐标 X
     */
    damageOffsetX() {
        return 120;
    }

    /**
     * 伤害的偏移坐标 Y
     */
    damageOffsetY() {
        return 40;
    }

}